/*
 * @lc app=leetcode.cn id=38 lang=cpp
 *
 * [38] 外观数列
 */

// @lc code=start
class Solution
{
public:
  string countAndSay(int n)
  {
    string last = "1";
    for (int i = 1; i < n; ++i) {
      string tmp;
      int L = 0, len = last.size();
      while (L < len) {
        int R = L;
        while (R < len && last[R] == last[L])
          ++R;
        tmp += to_string(R - L);
        tmp.push_back(last[L]);
        L = R;
      }
      last = tmp;
    }
    return last;
  }
};
// @lc code=end
